package org.lesBonsComptes.android.feedPlayer;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import org.lesBonsComptes.android.feedPlayer.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
 
public class SingleListItem extends Activity{
	Intent i = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.single_list_item_view);
        i = getIntent();
        

        
        
        Event currentEvent = null;
        SubEvent subEvent = null;
        EditText txtName = (EditText) findViewById(R.id.editNameSubEvent);
        EditText txtPrice = (EditText) findViewById(R.id.editPriceSubEvent);

        final ArrayList<Event> events = ContainerData.getEvents();
        
        for (Event event : events) {
        	if (String.valueOf(event.getId()).equals(i.getStringExtra("eventId"))) {
        		currentEvent = event;
        		for (SubEvent se : event.getSubEvents()) {
        			if (String.valueOf(se.getId()).equals(i.getStringExtra("subEventId"))) {
        				subEvent = se;
        			}
        		}
        	}
        }
        Log.e("feedPlayer",subEvent.toString());

        
        ArrayList<User> users = currentEvent.getUsers();
              
        // displaying selected product name
        txtName.setText(subEvent.getName());
        txtPrice.setText(String.valueOf(subEvent.getPrice()));
        

        txtName.addTextChangedListener(new TextWatcher() {

        	public void afterTextChanged(Editable s) {

            	try {
              		 
       				URL url = new URL("http://jeremy.alwaysdata.net/android/services.php?updateName="+s.toString()+"&idSubEvent="+i.getStringExtra("subEventId"));
       				try {
     						url.openStream();
     						
     					} catch (IOException e) {
     						e.printStackTrace();
     					}
       			} catch (MalformedURLException e1) {
       				e1.printStackTrace();
       			}
            }


        	public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				
			}

			
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				
			}
        });
        txtPrice.addTextChangedListener(new TextWatcher() {
            
            public void afterTextChanged(Editable s) {
            	
            	try {
           		 
       				URL url = new URL("http://jeremy.alwaysdata.net/android/services.php?updatePrice="+s.toString()+"&idSubEvent="+i.getStringExtra("subEventId"));
       				try {
     						url.openStream();
     						
     					} catch (IOException e) {
     						e.printStackTrace();
     					}
       			} catch (MalformedURLException e1) {
       				e1.printStackTrace();
       			}
            }

			
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				// TODO Auto-generated method stub
				
			}

			
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				
			}
        });
        // displaying the event users as radio button so you choose the payer
        RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
        
        for (User user : users) {
        	RadioButton rb = new RadioButton(this);
        	rb.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            rb.setText(user.getLogin());
            rb.setId((int) user.getId());
            if (user.getId() == subEvent.getPayer().getId()) {
            	rb.setChecked(true);
            }

            rg.addView(rb);
        }
        // on regarde si on change de payer
        rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            
            public void onCheckedChanged(RadioGroup RG, int checkID) {
                    // on change le payer donc faut le changer en bdd
            		// => dans checkID il y a la valeur de l'ID de l'user :)
              	 try {
            		 
       				URL url = new URL("http://jeremy.alwaysdata.net/android/services.php?addPayer="+checkID+"&idSubEvent="+i.getStringExtra("subEventId"));
       				try {
     						url.openStream();
     						
     					} catch (IOException e) {
     						e.printStackTrace();
     					}
       			} catch (MalformedURLException e1) {
       				e1.printStackTrace();
       			}
            }
        });
        
        
     // displaying the event users as radio button so you choose the buyer
        LinearLayout layoutBuyers = (LinearLayout) findViewById(R.id.linearLayout5);
        layoutBuyers.setOrientation(LinearLayout.VERTICAL);  //Can also be done in xml by android:orientation="vertical"
        for (User user : users) {
            LinearLayout row = new LinearLayout(this);
            row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

            CheckBox cb = new CheckBox(this);
            cb.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            cb.setText(user.getLogin());
            cb.setId((int) user.getId());
            row.addView(cb);
            for(User buyer : subEvent.getBuyers()) {
                if (user.getId() == buyer.getId()) {
                	cb.setChecked(true);
                }	
            }
            cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            	   
            	   public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
            		   if (isChecked) {
            			   // il faut ajouter l'user aux buyers (id de l'user : buttonView.getId())
            			   try {
                      		 
                  				URL url = new URL("http://jeremy.alwaysdata.net/android/services.php?addBuyer="+buttonView.getId()+"&idSubEvent="+i.getStringExtra("subEventId"));
                  				try {
                						url.openStream();
                						
                					} catch (IOException e) {
                						e.printStackTrace();
                					}
                  			} catch (MalformedURLException e1) {
                  				e1.printStackTrace();
                  			}
            		   }
            		   else {
            			   // faut enlever l'user des buyers (id de l'user : buttonView.getId())
            			   try {
                      		 
                  				URL url = new URL("http://jeremy.alwaysdata.net/android/services.php?removeBuyer="+buttonView.getId()+"&idSubEvent="+i.getStringExtra("subEventId"));
                  				try {
                						url.openStream();
                						
                					} catch (IOException e) {
                						e.printStackTrace();
                					}
                  			} catch (MalformedURLException e1) {
                  				e1.printStackTrace();
                  			}
            		   }
            	   }
            });
            
            layoutBuyers.addView(row);
        }
    }
}
